package com.Shoping.dao.impl;

import com.Shoping.dao.CommodityDao;
import com.Shoping.entity.Commodity;
import com.Shoping.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class CommodityDaoImpl implements CommodityDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public List<Commodity> selectByCgId(Integer cgId) throws SQLException {
        String sql = "SELECT cm_id cmId,cg_id cgId,cm_name cmName,cm_price cmPrice,cm_details cmDetails,cm_img cmImg FROM commodity WHERE cg_id=?;";
        return queryRunner.query(DbUtils.getConnection(),sql,new BeanListHandler<>(Commodity.class),cgId);
    }

    @Override
    public Commodity selectByCmId(Integer cmId) throws SQLException {
        String sql = "SELECT cm_id cmId,cg_id cgId,cm_name cmName,cm_price cmPrice,cm_details cmDetails,cm_img cmImg FROM commodity WHERE cm_id=?;";
        return queryRunner.query(DbUtils.getConnection(),sql,new BeanHandler<>(Commodity.class),cmId);
    }
}
